package com.yic.module.mes.controller.admin.processrouteinfo;

import com.yic.module.mes.controller.admin.processRoute.vo.ProcessRouteRespVO;
import com.yic.module.mes.convert.processRoute.ProcessRouteConvert;
import com.yic.module.mes.dal.dataobject.processRoute.ProcessRouteDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.mes.controller.admin.processrouteinfo.vo.*;
import com.yic.module.mes.dal.dataobject.processrouteinfo.ProcessRouteInfoDO;
import com.yic.module.mes.convert.processrouteinfo.ProcessRouteInfoConvert;
import com.yic.module.mes.service.processrouteinfo.ProcessRouteInfoService;

@Tag(name = "管理后台 - 工艺路线详细配置")
@RestController
@RequestMapping("/mes/process-route-info")
@Validated
public class ProcessRouteInfoController {

    @Resource
    private ProcessRouteInfoService processRouteInfoService;

    @PostMapping("/config")
    @Operation(summary = "创建工艺路线详细配置")
    @PreAuthorize("@ss.hasPermission('mes:process-route:config')")
    public CommonResult<Boolean> configProcessRouteInfo(@Valid @RequestBody List<ProcessRouteInfoCreateReqVO> createReqVOs) {
        processRouteInfoService.batchCreateProcessRouteInfo(createReqVOs);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获得工艺路线配置列表")
    @Parameter(name = "id", description = "", required = true, example = "1024,2048")
    public CommonResult<List<ProcessRouteInfoRespVO>> getProcessRouteListByRouteId(@RequestParam("id") Long id) {
        List<ProcessRouteInfoDO> list = processRouteInfoService.getProcessRouteListByRouteId(id);
        return success(ProcessRouteInfoConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/create")
    @Operation(summary = "创建工艺路线详细配置")
    @PreAuthorize("@ss.hasPermission('mes:process-route-info:create')")
    public CommonResult<Long> createProcessRouteInfo(@Valid @RequestBody ProcessRouteInfoCreateReqVO createReqVO) {
        return success(processRouteInfoService.createProcessRouteInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工艺路线详细配置")
    @PreAuthorize("@ss.hasPermission('mes:process-route-info:update')")
    public CommonResult<Boolean> updateProcessRouteInfo(@Valid @RequestBody ProcessRouteInfoUpdateReqVO updateReqVO) {
        processRouteInfoService.updateProcessRouteInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工艺路线详细配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:process-route-info:delete')")
    public CommonResult<Boolean> deleteProcessRouteInfo(@RequestParam("id") Long id) {
        processRouteInfoService.deleteProcessRouteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工艺路线详细配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:process-route-info:query')")
    public CommonResult<ProcessRouteInfoRespVO> getProcessRouteInfo(@RequestParam("id") Long id) {
        ProcessRouteInfoDO processRouteInfo = processRouteInfoService.getProcessRouteInfo(id);
        return success(ProcessRouteInfoConvert.INSTANCE.convert(processRouteInfo));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工艺路线详细配置分页")
    @PreAuthorize("@ss.hasPermission('mes:process-route-info:query')")
    public CommonResult<PageResult<ProcessRouteInfoRespVO>> getProcessRouteInfoPage(@Valid ProcessRouteInfoPageReqVO pageVO) {
        PageResult<ProcessRouteInfoDO> pageResult = processRouteInfoService.getProcessRouteInfoPage(pageVO);
        return success(ProcessRouteInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工艺路线详细配置 Excel")
    @PreAuthorize("@ss.hasPermission('mes:process-route-info:export')")
    @OperateLog(type = EXPORT)
    public void exportProcessRouteInfoExcel(@Valid ProcessRouteInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProcessRouteInfoDO> list = processRouteInfoService.getProcessRouteInfoList(exportReqVO);
        // 导出 Excel
        List<ProcessRouteInfoExcelVO> datas = ProcessRouteInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工艺路线详细配置.xls", "数据", ProcessRouteInfoExcelVO.class, datas);
    }

}
